<?php
class upload extends CI_Controller {


    function __construct()

    {

        parent::__construct();

        $this->load->helper('form');
        
        $this->load->helper('url');

    }  

    function index()

    {

    }
    
    function uploadCtrl(){
        $this->load->library('session');
        if($this->session->userdata('validated')){
          $this->load->library('form_validation');
          $this->form_validation->set_rules('rate','Oran','trim|xss_clean|required');
          if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
          $this->form_validation->set_rules('sharetext','Tahmins Yazı Alanı','trim|xss_clean');
          }else{
          $this->form_validation->set_rules('sharetext','Tahmins Yazı Alanı','trim|xss_clean|required');   
          }
          if($this->form_validation->run() == FALSE){
            $data['base']= $this->config->item('base_url');
            $data['css']=  $this->config->item('css');
            $data['title']= 'Tahmins.com..';
            $data['page']='content/share.php';
            $this->load->view('page_view',$data); 
          }else{
             if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])) { 
              if($this->uploadImage()){
               redirect('user/allTahmins');
              }else{
                $data['base']= $this->config->item('base_url');
                $data['css']=  $this->config->item('css');
                $data['title']= 'Tahmins.com..';
                $data['page']='content/share.php';
                $data['errors'] = 'Bir hata oluştu.Lütfen tekrar deneyiniz..';
                $this->load->view('page_view',$data);  
              }
          }else{
              $this->load->model('tahminsModel');
              if($ex = $this->tahminsModel->addTahmNoImg()){
                  redirect('user/allTahmins');
              }else{
                $data['base']= $this->config->item('base_url');
                $data['css']=  $this->config->item('css');
                $data['title']= 'Tahmins.com..';
                $data['page']='content/share.php';
                $data['errors'] = 'Bir hata oluştu.Lütfen tekrar deneyiniz..';
                $this->load->view('page_view',$data);
              }
          }
              }
          
        }else{
            redirect('login/loginn');
        }
    }
    //Upload Image function

    function uploadImage()

    {
        $this->load->library('session');
        if($this->session->userdata('validated')){
       $config['upload_path']   =   "uploads/";

       $config['allowed_types'] =   "gif|jpg|jpeg|png"; 

       $config['max_size']      =   "5000";

       $config['max_width']     =   "1907";

       $config['max_height']    =   "1280";
       
       $config['file_name']     =   md5($this->session->userdata('kadi')).'_'.round(microtime(true) * 1000);

       $this->load->library('upload',$config);

       if(!$this->upload->do_upload())

       {

           echo $this->upload->display_errors();
           return FALSE;

       }

       else

       {

           $finfo=$this->upload->data();

           $this->_createThumbnail($finfo['file_name']);

           $data['uploadInfo'] = $finfo;

           $data['thumbnail_name'] = $finfo['raw_name']. '_thumb' .$finfo['file_ext']; 
           
           $this->load->model('tahminsModel');
           if($qu = $this->tahminsModel->addTahm($finfo['raw_name'],$finfo['file_ext'])){
           return TRUE;
           }else {
               return FALSE;
           }
       }
       }else{
           redirect('login/loginn');
       }
    }

    //Create Thumbnail function

    function _createThumbnail($filename)

    {

        $config['image_library']    = "gd2";      

        $config['source_image']     = "uploads/" .$filename;      

        $config['create_thumb']     = TRUE;      

        $config['maintain_ratio']   = TRUE;
        
        $config['width'] = 764;      

        $config['height'] = 500;
        
        $dim = (intval($filename["image_width"]) / intval($filename["image_height"])) - ($config['width'] / $config['height']);
        $config['master_dim'] = ($dim > 0)? "height" : "width";

        $this->load->library('image_lib',$config);

        if(!$this->image_lib->resize())

        {

            echo $this->image_lib->display_errors();

        }      

    }

  }



?>
